﻿#region Copyright © 2008-2009, DevExpressContrib (MIT License)
// This software is licensed under The MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion

namespace Test.Xpo.UpdateSchema
{
    using System;
    using DevExpress.Xpo;
    using DevExpressContrib.Xpo.UpdateSchema.Interface;

    /// <summary>
    /// DataStoreProperties is an XPO object to store properties of the data store.
    /// </summary>
    public class DataStoreProperties : XPObject
    {

        private int schemaVersion;
        private DateTime schemaVersionUpdated;


        /// <summary>
        /// Initializes a new instance of the <see cref="DataStoreProperties"/> class.
        /// </summary>
        /// <param name="session">The session used to create the instance.</param>
        public DataStoreProperties(Session session) : base(session) { }


        /// <summary>
        /// Gets or sets the schema version.
        /// </summary>
        /// <value>The schema version.</value>
        public int SchemaVersion
        {
            get { return schemaVersion; }
            set { SetPropertyValue("SchemaVersion", ref schemaVersion, value); }
        }

        /// <summary>
        /// Gets or sets the DateTime the schema version was updated.
        /// </summary>
        /// <value>The DateTime the schema version was updated.</value>
        public DateTime SchemaVersionUpdated
        {
            get { return schemaVersionUpdated; }
            set { SetPropertyValue("SchemaVersionUpdated", ref schemaVersionUpdated, value); }
        }
    }

    /// <summary>
    /// The object used to update the schema version. Also provides information
    /// about the schema version.
    /// </summary>
    public class SchemaVersionInt : SchemaVersionBase<int>
    {

        /// <summary>
        /// Increment the requiredSchemaVersion value for each version of the schema
        /// </summary>
        private int requiredSchemaVersion = 1;


        /// <summary>
        /// Initializes a new instance of the <see cref="SchemaVersionInt"/> class.
        /// </summary>
        /// <param name="dataLayer">The XPO data layer to use when accessing the data store.</param>
        public SchemaVersionInt(IDataLayer dataLayer) : base(dataLayer) { }


        /// <summary>
        /// Gets the property name of the DateTime that the SchemaVersion was updated.
        /// If this is null or empty, no attempt will be made to store the updated time.
        /// </summary>
        /// <value>
        /// The property name of the DateTime that the SchemaVersion was updated.
        /// </value>
        public override string DateVersionUpdatedPropertyName { get { return "SchemaVersionUpdated"; } }

        /// <summary>
        /// Gets the name of the SchemaVersion property in the data store.
        /// </summary>
        /// <value>The name of the SchemaVersion property.</value>
        public override string VersionPropertyName { get { return "SchemaVersion"; } }

        /// <summary>
        /// Gets the type of the XPO object from the data store. This must be a
        /// descendant of XPObjectBase.
        /// </summary>
        /// <value>The type of the XPO object.</value>
        public override Type XpoObjectType { get { return typeof(DataStoreProperties); } }


        /// <summary>
        /// Gets the required schema version (as an object). This value is what
        /// the schema version should be for the current version of the application.
        /// </summary>
        /// <returns>
        /// The value (as an object) of the required data source schema version.
        /// </returns>
        public override int GetRequiredSchemaVersion() { return requiredSchemaVersion; }
    }
}
